package com.hot12345.springmvc.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hot12345.common.spring.SpringAbstractController;
import com.hot12345.common.util.HtmlUtil;
import com.hot12345.dao.SiteDaoImpl;
import com.hot12345.entity.Site;

@Controller
@RequestMapping("/admin/accessExtSite/")
public class AccessExternalSiteController extends SpringAbstractController {

	@RequestMapping("/index")
	@ResponseBody
	public  Map<String, Object> getPageInfo(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		String url = request.getParameter("url");
		HtmlUtil htmlUtil = new HtmlUtil();
		boolean flag = htmlUtil.getPageInfo(url);
		
		if(!flag){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("success", -1);
			map.put("message", "获取信息失败");
			map.put("data", "{}");
			return map;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", 1);
		map.put("message", "获取信息成功");
		Map<String, Object> infoMap = new HashMap<String, Object>();
		infoMap.put("title", htmlUtil.getTitle());
		infoMap.put("domain", htmlUtil.getDomian());
		SiteDaoImpl siteDao = SiteDaoImpl.getInstance();
		
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("domain", htmlUtil.getDomian());
		params.put("state", 1);
		Site site = siteDao.findUniqueByParams(params);
		if(site!=null){
			infoMap.put("siteID", site.getId());
			infoMap.put("siteName", site.getAbbreviation());
		}
		map.put("data", infoMap);
		
		return map;
	}
}
